import time

from selenium.webdriver.common.by import By

from common.Util import BlogDriver

class BlogList:
    driver = ""
    url = ""
    def __init__(self):
        self.url = "http://8.137.19.140:9090/blog_list.html"
        self.driver = BlogDriver.driver
        self.driver.get(self.url)

    def BlogLoginListTest(self):  # 登路状态下
        # 检查头像
        self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.left > div > img")
        # 检查用户名
        self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.left > div > h3")
        # 检查GitHub链接
        self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.left > div > a")
        # 检查文章
        self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.left > div > div:nth-child(4) > span:nth-child(1)")
        # 检查文章数量
        self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.left > div > div:nth-child(5) > span:nth-child(1)")
        # 检查分类
        self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.left > div > div:nth-child(4) > span:nth-child(2)")
        # 检查分类数量
        self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.left > div > div:nth-child(5) > span:nth-child(2)")
        # 检查博客标题
        self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.right > div:nth-child(1) > div.title")
        # 检查博客发布时间
        self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.right > div:nth-child(1) > div.date")
        # 检查博客正文内容
        self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.right > div:nth-child(1) > div.desc")
        # 检查查看全文按钮
        self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.right > div:nth-child(1) > a")

        BlogDriver.GetScreenShot()
        print("列表页元素检查无误")
        # self.driver.quit()


    def NotBlogLoginListTest(self):  # 未登录状态下
        self.driver.get("http://8.137.19.140:9090/blog_list.html")
        time.sleep(2)
        BlogDriver.GetScreenShot()
        # 检查到看登录框说明访问后跳转回登录页面
        Quittitle = self.driver.title
        QuitUrl = self.driver.current_url
        print(Quittitle)
        print(QuitUrl)
        assert Quittitle == "博客登陆页"
        assert QuitUrl == "http://8.137.19.140:9090/blog_login.html"
        print("检查到回到登录页面")
        self.driver.quit()


    def QuitBlogLoginListText(self):  # 登录博客状态下测试退出
        # 点击退出按钮
        self.driver.find_element(By.CSS_SELECTOR, "body > div.nav > a:nth-child(6)").click()
        BlogDriver.GetScreenShot()

        # 判断一下退出后的页面url和页面的标题
        Quittitle = self.driver.title
        QuitUrl = self.driver.current_url
        print(Quittitle)
        print(QuitUrl)
        assert Quittitle == "博客登陆页"
        assert QuitUrl == "http://8.137.19.140:9090/blog_login.html"

        # 判断一下账号输入框和密码输入框的内容是否为空
        Quitusername = self.driver.find_element(By.CSS_SELECTOR, "#username").text
        Quitpassword = self.driver.find_element(By.CSS_SELECTOR, "#password").text
        print("Quitusername" + Quitusername)
        print("Quitpassword" + Quitpassword)
        assert Quitusername == ""
        assert Quitpassword == ""
        print("注销成功")

